﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Appsters
{
    public partial class drone : Form
    {
        private Timer timer = new Timer();
        private Timer sTimer = new Timer();
        private Timer mTimer = new Timer();
        private Timer nTimer = new Timer();
        private DateTime longDate;
        private string alarmValue;
        private string t;
        private string s;
        private string d;
        private string r;
        private string i;
        private int c;

        public drone(DateTime lDate, string alm, string t ,string s, string d, string r, string i)
        {
            this.t = t;
            this.s = s;
            this.d = d;
            this.r = r;
            this.i = i;

            InitializeComponent();
            this.Hide();

            this.longDate = lDate;
            this.alarmValue = alm;

            if (alm == "No Alarm")
            {
                this.Close();
            }
            else
            {
                if(this.r.Equals("0")){
                    startAlarm();
                }else if(Int32.Parse(r) > 0){
                    startAlarm();
                    c = Int32.Parse(r);                             
                    consecutiveAlarm(Int32.Parse(i), Int32.Parse(r));                    
                }
            }

        }

        private void startAlarm()
        {
            DateTime currentDate = DateTime.Now;
            string dl = longDate.ToString();
            string[] dla = dl.Split(' ');
            DateTime userDate = Convert.ToDateTime(dla[0]);

            DateTime datetime = DateTime.ParseExact(alarmValue, "h:mm tt", System.Globalization.CultureInfo.InvariantCulture);
            TimeSpan userTime = datetime.TimeOfDay;
            DateTime dline = userDate.Add(userTime);

            try
            {
                timer.Interval = (int)(dline - currentDate).TotalMilliseconds;
            }
            catch (ArgumentOutOfRangeException a)
            {
                MessageBox.Show("Enter proper alarm value.");
                DailyView dv = new DailyView();
                dv.Show();
                this.Hide();
            }
            
            timer.Tick += new EventHandler(showAlarmO);
            timer.Enabled = true;

        }

        private void showAlarmO(object sender, EventArgs e)
        {
            
            if(t.Length > 16){
                title.Text = t.Substring(0, 15) + "...";
            }
            else
            {
                title.Text = t;
            }

            if (s.Length > 16)
            {
                subj.Text = s.Substring(0, 15) + "...";
            }
            else
            {
                subj.Text = s;
            }

            if (d.Length > 16)
            {
                desc.Text = d.Substring(0, 15) + "...";
            }
            else
            {
                desc.Text = d;
            }        
    
            this.Show();
            timer.Stop();
        }       
             
        private void consecutiveAlarm(int ivl, int rep)
        {
            switch (rep)
            {
                case 3:
                    sTimer.Interval = ivl * 60000;
                    sTimer.Tick += new EventHandler(showAlarmR);
                    sTimer.Start();
                    break;
                case 2:
                    nTimer.Interval = ivl * 60000;
                    nTimer.Tick += new EventHandler(showAlarmN);
                    nTimer.Start();
                    break;
                case 1:
                    mTimer.Interval = ivl * 60000;
                    mTimer.Tick += new EventHandler(showAlarmL);
                    mTimer.Start();
                    break;
            }                                               
        }

        private void showAlarmR(object sender, EventArgs e)
        {
           
            if (t.Length > 16)
            {
                title.Text = t.Substring(0, 15) + "...";
            }
            else
            {
                title.Text = t;
            }

            if (s.Length > 16)
            {
                subj.Text = s.Substring(0, 15) + "...";
            }
            else
            {
                subj.Text = s;
            }

            if (d.Length > 16)
            {
                desc.Text = d.Substring(0, 15) + "...";
            }
            else
            {
                desc.Text = d;
            }

            this.Show();
            sTimer.Stop();
            c--;
            consecutiveAlarm(Int32.Parse(i), c);            
        }

        private void showAlarmN(object sender, EventArgs e)
        {
            
            if (t.Length > 16)
            {
                title.Text = t.Substring(0, 15) + "...";
            }
            else
            {
                title.Text = t;
            }

            if (s.Length > 16)
            {
                subj.Text = s.Substring(0, 15) + "...";
            }
            else
            {
                subj.Text = s;
            }

            if (d.Length > 16)
            {
                desc.Text = d.Substring(0, 15) + "...";
            }
            else
            {
                desc.Text = d;
            }

            this.Show();
            nTimer.Stop();
            c--;
            consecutiveAlarm(Int32.Parse(i), c);
        }

        private void showAlarmL(object sender, EventArgs e)
        {

            if (t.Length > 16)
            {
                title.Text = t.Substring(0, 15) + "...";
            }
            else
            {
                title.Text = t;
            }

            if (s.Length > 16)
            {
                subj.Text = s.Substring(0, 15) + "...";
            }
            else
            {
                subj.Text = s;
            }

            if (d.Length > 16)
            {
                desc.Text = d.Substring(0, 15) + "...";
            }
            else
            {
                desc.Text = d;
            }

            this.Show();
        }

        private void ok_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
